﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.DAL.Statistics;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.GroupMember;
using MINERP.COM;

namespace MINERP.BLL.Statistics
{
    public class PerformanceBLL
    {

        PerformanceDAL _dal = new PerformanceDAL();
        /// <summary>
        /// 取得全部业绩
        /// </summary>
        /// <param name="inStartTime"></param>
        /// <param name="inEndTime"></param>
        /// <returns></returns>
        public List<PerformanceView> GetPerformanceAll(int inGroupId, DateTime inStartTime, DateTime inEndTime)
        {
            List<PerformanceView> views = new List<PerformanceView>();
            List<users> listUser;
            if (inGroupId == 0)
            {
                listUser = _dal.GetAllUser();
            }
            else
            {
                listUser = _dal.GetAllUserByGroupId(inGroupId);
            }

            foreach (users us in listUser)
            {
                if (!us.tb_Tech.IsLoaded)
                {
                    us.tb_Tech.Load();
                }

                if (!us.tb_WorkerGroupReference.IsLoaded)
                {

                    us.tb_WorkerGroupReference.Load();
                }

                List<tb_Tech> ListTech = us.tb_Tech.ToList();

                foreach (tb_Tech tech in ListTech)
                {
                    PerformanceView view = new PerformanceView();
                    int orderCount;
                    int photoCount;
                    double photoAreas;
                    int result = _dal.GetPhotoCountOrderCountPhotoAreas(us.userID, tech.TechId, inStartTime, inEndTime, out orderCount, out photoCount, out photoAreas);

                    view.Departments = us.tb_WorkerGroup.GroupName;
                    view.UserName = us.UserName;
                    view.Tech = tech.TechName;
                    //view.OrderNum = _dal.GetProductNumByIdAndTech(us.userID, tech.TechId, inStartTime, inEndTime);
                    //view.PhotoNum = _dal.GetPhotoNumByIdAndTech(us.userID, tech.TechId, inStartTime, inEndTime);
                    //view.FileNum = view.PhotoNum;
                    //view.Areas = _dal.GetPhotoAreasByIdAndTech(us.userID, tech.TechId, inStartTime, inEndTime);
                    view.OrderNum = orderCount;
                    view.PhotoNum = photoCount;
                    view.FileNum = photoCount;
                    view.Areas = photoAreas;
                    views.Add(view);
                }

            }

            return views;
        }

        /// <summary>
        /// 获取所有组
        /// </summary>
        /// <returns></returns>
        public List<Tb_WorkerGroup_Info> GetAllGroup()
        {
            List<tb_WorkerGroup> listGroup = _dal.GetAllGroup();
            List<Tb_WorkerGroup_Info> rtnList = new List<Tb_WorkerGroup_Info>();
            Tb_WorkerGroup_Info frist = new Tb_WorkerGroup_Info();
            frist.GroupId = 0;
            frist.GroupName = "全部";
            rtnList.Add(frist);
            foreach (tb_WorkerGroup group in listGroup)
            {
                Tb_WorkerGroup_Info info = new Tb_WorkerGroup_Info() { GroupId = group.GroupId, GroupName = group.GroupName };
                rtnList.Add(info);
            }

            return rtnList;
        }


        public List<string> GetUserListByGroupId(int GroupId)
        {
            if (GroupId > 0)
            {
                return _dal.GetAllUserByGroupId(GroupId).Select(u => u.UserName).ToList();
            }
            else
            {
                return null;
            }
        }

        public List<PerformanceView> GetPerformanceAll(string groupName, DateTime startDate, DateTime endDate)
        {            
            List<EmployeePerformance> UserPerformanceList = _dal.getPerformanceAll(groupName, startDate, endDate);
            return ChangeEmployPerformanceToView(UserPerformanceList);
        }

        public List<PerformanceView> GetPerformanceAll(string groupName, string userName, DateTime startDate, DateTime endDate)
        {
            List<EmployeePerformance> UserPerformanceList = _dal.getPerformanceAll(groupName, userName, startDate, endDate);
            return ChangeEmployPerformanceToView(UserPerformanceList);         
        }

        //help method
        private List<PerformanceView> ChangeEmployPerformanceToView(List<EmployeePerformance> UserPerformanceList)
        {
            List<PerformanceView> views = new List<PerformanceView>();
            PerformanceView view;
            foreach (var item in UserPerformanceList)
            {
                view = new PerformanceView();
                view.Departments = item.Departments;
                view.UserName = item.UserName;
                view.Tech = item.Tech;
                view.OrderNum = item.OrderNum;
                view.PhotoNum = item.PhotoNum;          
                view.FileNum = item.PhotoNum;
                view.Areas = item.Areas;
                view.CompletedDate = item.CompleteDate;
                view.OutReturnAreas = _dal.GetOneManOutRerunByDay(item.Departments, item.UserName, item.Tech, item.CompleteDate, item.CompleteDate.AddDays(1));
                views.Add(view);
            }

            return views;
        }

        public List<PerformanceView> GetPerformanceAll(DateTime startDate, DateTime endDate)
        {
            List<EmployeePerformance> UserPerformanceList = _dal.getPerformanceAll(startDate, endDate);
            return ChangeEmployPerformanceToView(UserPerformanceList);
        }

        /// <summary>
        /// 获得员工业绩
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="dtStart"></param>
        /// <param name="dtEnd"></param>
        /// <returns></returns>
        public List<New_EmployeePerformanceBySpec_Info> GetEPByPhotoSpec(List<UserInfo> userList, DateTime dtStart, DateTime dtEnd)
        {
            List<New_EmployeePerformanceBySpec_Info> epList = new List<New_EmployeePerformanceBySpec_Info>();
            try
            {
                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    foreach (var userInfo in userList)
                    {


                        List<New_EmployeePerformanceBySpec> dbepList = db.New_EmployeePerformanceBySpec.Where(e => e.UserName == userInfo.UserName && e.CompleteDateTime >= dtStart && e.CompleteDateTime <= dtEnd).ToList();
                        foreach (var item in dbepList)
                        {
                            if (!item.New_EmployeePerformanceBySpecPhoto.IsLoaded)
                            {
                                item.New_EmployeePerformanceBySpecPhoto.Load();
                            }
                            New_EmployeePerformanceBySpec_Info epInfo = new New_EmployeePerformanceBySpec_Info()
                            {
                                CompleteDateTime = item.CompleteDateTime,
                                Id = item.Id,
                                OrderEditTime = item.OrderEditTime,
                                orderProductId = item.orderProductId,
                                orderProductName = item.orderProductName,
                                PageNum = item.PageNum,
                                ShopName = item.ShopName,
                                SpecName = item.SpecName,
                                Status = item.Status,
                                TechName = item.TechName,
                                UserId = item.UserId,
                                UserName = item.UserName,
                                epPhotoInfoList = new List<New_EmployeePerformanceBySpecPhoto_Info>()
                            };
                            foreach (var epphoto in item.New_EmployeePerformanceBySpecPhoto)
                            {

                                New_EmployeePerformanceBySpecPhoto_Info epPhotoInfo = new New_EmployeePerformanceBySpecPhoto_Info()
                                {
                                    Area = epphoto.Area,
                                    EmployeePerformanceBySpecId = epphoto.New_EmployeePerformanceBySpec.Id,
                                    Height = epphoto.Height,
                                    Num = epphoto.Num,
                                    photoId = epphoto.photoId,
                                    PhotoName = epphoto.PhotoName,
                                    Width = epphoto.Width,
                                };
                                epInfo.epPhotoInfoList.Add(epPhotoInfo);
                            }
                            epList.Add(epInfo);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return epList;
        }


        public List<UserInfo> GetUserByGroupId(int GroupId)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                if (GroupId == 0)
                {
                    return ConvertUserToInfo(db.users.Where(u => u.IsUseful == true).ToList());
                }
                else
                {
                    return ConvertUserToInfo(db.users.Where(u => u.tb_WorkerGroup.GroupId == GroupId && u.IsUseful == true).ToList());
                }
            }
        }

        private List<UserInfo> ConvertUserToInfo(List<users> uList)
        {
            List<UserInfo> result = new List<UserInfo>();

            foreach (var u in uList)
            {
                result.Add(new UserInfo()
                {
                    UserID = u.userID,
                    UserName = u.UserName

                });
            }

            return result;
        }
    }
}
